#############################################################################
# FileManager plugin for Rhythmbox
# Copyright (C) 2008 Dmitry Teslenko
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#############################################################################

import gtk
import gobject

class SelectFolderDialog (gtk.Dialog):
	def __init__(self, title, parent = None):
		gtk.Dialog.__init__(self, title, parent, gtk.DIALOG_MODAL,
			(gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT,
			gtk.STOCK_OK, gtk.RESPONSE_ACCEPT))

		self.__get_caption_func = None

		self.__chooser = gtk.FileChooserButton(title)
		self.__chooser.set_action(gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER)
		self.__chooser.connect('current-folder-changed', 
			self.update_current_folder)

		self.__mask_entry = gtk.Entry()
		self.__mask_entry.connect('changed', self.update_mask)

		self.__pre_store = gtk.ListStore(gobject.TYPE_STRING)
		view = gtk.TreeView(self.__pre_store)
		view.set_headers_visible(False)

		window = gtk.ScrolledWindow()
		window.add_with_viewport(view)
		window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)

		cell = gtk.CellRendererText()
		column = gtk.TreeViewColumn('Preview', cell)
		view.append_column(column)
		column.add_attribute(cell, 'text', 0)

		self.vbox.pack_start(self.__chooser, expand = False)
		self.vbox.pack_start(self.__mask_entry, expand = False)
		self.vbox.pack_start(window)
		self.__chooser.show()
		self.__mask_entry.show()
		window.show_all()

		self.set_size_request(500, 200)
		return

	def get_folder_path(self):
		return self.__chooser.get_current_folder()

	def get_mask(self):
		return self.__mask_entry.get_text()

	def set_mask(self, mask):
		self.__mask_entry.set_text(mask)
		return

	def set_get_caption_func(self, get_caption_func):
		self.__get_caption_func = get_caption_func
		return
	
	def set_folder_path(self, folder_path):
		self.__chooser.set_current_folder(folder_path)
		return

	def update_current_folder(self, file_chooser):
		self.update_pre_store()
		return

	def update_mask(self, entry):
		self.update_pre_store()
		return

	def update_pre_store(self):
		m = self.__mask_entry.get_text()
		f = self.__chooser.get_current_folder()

		self.__pre_store.clear()
		for i in range(5):
			s = ''
			if self.__get_caption_func:
				s = self.__get_caption_func(i, f, m)
			self.__pre_store.append([s])
		return

def test_func(f, m):
	s = '%s, %s' % (f, m)
	return s

if __name__ == '__main__':
	s = SelectFolderDialog('Select folder')
	s.set_mask('%a/%t')
	s.set_get_caption_func(test_func)

	r = s.run() 
	s.hide()
	if r == gtk.RESPONSE_ACCEPT:
		print s.get_folder_path()
		print s.get_mask()
